import request from '@/utils/request'

// ==================== 仓库管理 ====================

// 获取仓库列表
export function getRepositories(params) {
  return request({
    url: '/api/zenithMind/version/repositories',
    method: 'get',
    params
  })
}

// 获取仓库详情
export function getRepositoryDetail(id) {
  return request({
    url: `/api/zenithMind/version/repositories/${id}`,
    method: 'get'
  })
}

// 创建仓库
export function createRepository(data) {
  return request({
    url: '/api/zenithMind/version/repositories',
    method: 'post',
    data
  })
}

// 更新仓库
export function updateRepository(id, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${id}`,
    method: 'put',
    data
  })
}

// 删除仓库
export function deleteRepository(id) {
  return request({
    url: `/api/zenithMind/version/repositories/${id}`,
    method: 'delete'
  })
}

// 克隆仓库
export function cloneRepository(id, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${id}/clone`,
    method: 'post',
    data
  })
}

// 获取我的仓库
export function getMyRepositories(params) {
  return request({
    url: '/api/zenithMind/version/repositories/my',
    method: 'get',
    params
  })
}

// 搜索仓库
export function searchRepositories(keyword, params) {
  return request({
    url: '/api/zenithMind/version/repositories/search',
    method: 'get',
    params: { keyword, ...params }
  })
}

// ==================== 分支管理 ====================

// 获取分支列表
export function getBranches(repositoryId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/branches`,
    method: 'get'
  })
}

// 创建分支
export function createBranch(repositoryId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/branches`,
    method: 'post',
    data
  })
}

// 删除分支
export function deleteBranch(repositoryId, branchName) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/branches/${branchName}`,
    method: 'delete'
  })
}

// 切换分支
export function switchBranch(repositoryId, branchName) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/branches/${branchName}/switch`,
    method: 'post'
  })
}

// 合并分支
export function mergeBranch(repositoryId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/branches/merge`,
    method: 'post',
    data
  })
}

// ==================== 提交管理 ====================

// 获取提交历史
export function getCommits(repositoryId, params) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/commits`,
    method: 'get',
    params
  })
}

// 获取提交详情
export function getCommitDetail(repositoryId, commitId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/commits/${commitId}`,
    method: 'get'
  })
}

// 创建提交
export function createCommit(repositoryId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/commits`,
    method: 'post',
    data
  })
}

// 回滚提交
export function revertCommit(repositoryId, commitId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/commits/${commitId}/revert`,
    method: 'post'
  })
}

// 比较提交
export function compareCommits(repositoryId, commit1, commit2) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/commits/compare`,
    method: 'get',
    params: { commit1, commit2 }
  })
}

// ==================== 文件管理 ====================

// 获取文件列表
export function getRepositoryFiles(repositoryId, params) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/files`,
    method: 'get',
    params
  })
}

// 获取文件内容
export function getFileContent(repositoryId, filePath, params) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/files/content`,
    method: 'get',
    params: { filePath, ...params }
  })
}

// 创建文件
export function createFile(repositoryId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/files`,
    method: 'post',
    data
  })
}

// 更新文件
export function updateFile(repositoryId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/files`,
    method: 'put',
    data
  })
}

// 删除文件
export function deleteFile(repositoryId, filePath) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/files`,
    method: 'delete',
    data: { filePath }
  })
}

// 上传文件
export function uploadFile(repositoryId, formData) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/files/upload`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取文件历史
export function getFileHistory(repositoryId, filePath, params) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/files/history`,
    method: 'get',
    params: { filePath, ...params }
  })
}

// ==================== 标签管理 ====================

// 获取标签列表
export function getTags(repositoryId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/tags`,
    method: 'get'
  })
}

// 创建标签
export function createTag(repositoryId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/tags`,
    method: 'post',
    data
  })
}

// 删除标签
export function deleteTag(repositoryId, tagName) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/tags/${tagName}`,
    method: 'delete'
  })
}

// ==================== 拉取请求管理 ====================

// 获取拉取请求列表
export function getPullRequests(repositoryId, params) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/pull-requests`,
    method: 'get',
    params
  })
}

// 创建拉取请求
export function createPullRequest(repositoryId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/pull-requests`,
    method: 'post',
    data
  })
}

// 获取拉取请求详情
export function getPullRequestDetail(repositoryId, prId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/pull-requests/${prId}`,
    method: 'get'
  })
}

// 合并拉取请求
export function mergePullRequest(repositoryId, prId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/pull-requests/${prId}/merge`,
    method: 'post',
    data
  })
}

// 关闭拉取请求
export function closePullRequest(repositoryId, prId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/pull-requests/${prId}/close`,
    method: 'post'
  })
}

// ==================== 问题管理 ====================

// 获取问题列表
export function getIssues(repositoryId, params) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/issues`,
    method: 'get',
    params
  })
}

// 创建问题
export function createIssue(repositoryId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/issues`,
    method: 'post',
    data
  })
}

// 获取问题详情
export function getIssueDetail(repositoryId, issueId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/issues/${issueId}`,
    method: 'get'
  })
}

// 更新问题
export function updateIssue(repositoryId, issueId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/issues/${issueId}`,
    method: 'put',
    data
  })
}

// 关闭问题
export function closeIssue(repositoryId, issueId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/issues/${issueId}/close`,
    method: 'post'
  })
}

// ==================== 协作者管理 ====================

// 获取协作者列表
export function getCollaborators(repositoryId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/collaborators`,
    method: 'get'
  })
}

// 添加协作者
export function addCollaborator(repositoryId, data) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/collaborators`,
    method: 'post',
    data
  })
}

// 移除协作者
export function removeCollaborator(repositoryId, userId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/collaborators/${userId}`,
    method: 'delete'
  })
}

// 更新协作者权限
export function updateCollaboratorPermission(repositoryId, userId, permission) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/collaborators/${userId}/permission`,
    method: 'put',
    data: { permission }
  })
}

// ==================== 仓库操作 ====================

// 收藏仓库
export function starRepository(repositoryId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/star`,
    method: 'post'
  })
}

// 取消收藏仓库
export function unstarRepository(repositoryId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/star`,
    method: 'delete'
  })
}

// Fork 仓库
export function forkRepository(repositoryId) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/fork`,
    method: 'post'
  })
}

// 保护分支
export function protectBranch(repositoryId, branchName) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/branches/${branchName}/protect`,
    method: 'post'
  })
}

// 取消保护分支
export function unprotectBranch(repositoryId, branchName) {
  return request({
    url: `/api/zenithMind/version/repositories/${repositoryId}/branches/${branchName}/protect`,
    method: 'delete'
  })
}

// 获取仓库提交记录（别名）
export function getRepositoryCommits(repositoryId, params) {
  return getCommits(repositoryId, params)
}
